package com.itheima.system.controller;

import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.system.pojo.DeviceManage;
import com.itheima.system.service.DeviceManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/deviceManage")
public class DeviceManageController {

    @Autowired
    private DeviceManageService deviceManageService;


    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<DeviceManage> deviceManageList = deviceManageService.findAll();
        if (deviceManageList==null){
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
        return new Result(true,StatusCode.OK,"查询成功",deviceManageList);
    }

    /**
     * 根据ID查询
     * @return
     */
    @GetMapping("/findById/{did}")
    public Result findById(@PathVariable Integer did){
        List<DeviceManage> deviceManageList = deviceManageService.findById(did);
        System.out.println(deviceManageList);
        if (deviceManageList.isEmpty()&&deviceManageList.size()==0){
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
        return new Result(true,StatusCode.OK,"111",deviceManageList);
    }

    /**
     * 添加
     * @param deviceManage
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody DeviceManage deviceManage){
        System.out.println(deviceManage);
        try {
            deviceManageService.add(deviceManage);
            return new Result(true,StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    /**
     * 修改
     * @param deviceManage
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody DeviceManage deviceManage){
        System.out.println(deviceManage);
        Integer integer = deviceManageService.update(deviceManage);
        if (integer==0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }
}
